package cn.wolfcode.crm.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.SystemDictionaryItemQueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.RequiredPermission;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
	@Autowired
	private ISystemDictionaryItemService systemDictionaryItemService;
	
	@Autowired
	private ISystemDictionaryService systemDictionaryService;
	
	@RequiredPermission({"字典明细列表", "systemDictionaryItem:list"})
	@RequestMapping("/list")
	public String list(@ModelAttribute("qo") SystemDictionaryItemQueryObject qo, Model model, Long parentId) {
		PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.list(qo);
		if (parentId != null) {
			qo.setParentId(parentId);
		}
		List<SystemDictionary> doinfo = systemDictionaryService.selectAllNotPage();
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("doinfo", doinfo);
		return "systemDictionaryItem/list";
	}

	@RequiredPermission({"字典明细编辑", "systemDictionaryItem:input"})
	@RequestMapping("/input")
	public String input(Long id, Model model) {
		if (id != null) {
			model.addAttribute("entity", systemDictionaryItemService.get(id));
		}
		return "systemDictionaryItem/input";
	}

	@RequiredPermission({"字典明细新增/更新", "systemDictionaryItem:saveOrUpdate"})
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(SystemDictionaryItem entity) {
		systemDictionaryItemService.saveOrUpdate(entity);
		return "redirect:/systemDictionaryItem/list.do";
	}
	
	@RequiredPermission({"字典明细删除", "systemDictionaryItem:delete"})
	@RequestMapping("/delete")
	public String delete(@RequestParam("id") Long id) {
		if (id != null) {
			systemDictionaryItemService.delete(id);
		}
		return "redirect:/systemDictionaryItem/list.do";
	}
}
